package proj.unions.game;

import java.io.IOException;
import java.io.InputStream;

import proj.unions.cppmanager.CPPManager;
import proj.unions.general.GameView;
import proj.unions.general.L;
import proj.unions.general.SuperTools;
import proj.unions.general.Text;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class ThirdLogin {

	final public static boolean isThirdLogin = false;
	private static String loginParameter = "";	//登陆参数
	
	private RelativeLayout mRelativeLayout = null;
	private Activity mActivity = null;
	
	private ThirdLogin(RelativeLayout l)
	{
		mRelativeLayout = l;
		mActivity = SuperTools.getInstance().getCurActivity();
//		setWidget();
	}
	
	private void setWidget()
	{
		int w = SuperTools.getInstance().getWidth();
		int h = SuperTools.getInstance().getHeight();
		
		float wScale = (float)w / (float)1280;
		float hScale = (float)h / (float)720;
		
		try {
			InputStream is = mActivity.getAssets().open("loginBg.png");
			Bitmap loginBgBM = GameView.getNewBitmap(BitmapFactory.decodeStream(is));
			ImageView loginBg = new ImageView(mActivity);
			loginBg.setImageBitmap(loginBgBM);
			mRelativeLayout.addView(loginBg, new LayoutParams(w, h));
			
			is.reset();
			is = mActivity.getAssets().open("loginBtn.png");
			Bitmap loginBtnBM = GameView.getNewBitmap(BitmapFactory.decodeStream(is), 0.95f * wScale, 0.95f * wScale);
			int loginBtnLen = loginBtnBM.getWidth();
			ImageView loginBtn = new ImageView(mActivity);
			loginBtn.setImageBitmap(loginBtnBM);
			mRelativeLayout.addView(loginBtn, new LayoutParams(loginBtnBM.getWidth(), loginBtnBM.getHeight()));
			loginBtn.setX(_tw(640) - loginBtnLen / 2);
			loginBtn.setY(_th(720/5*4));
			loginBtn.setOnClickListener(btn_Login);
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public int _tw(int origina_lw)
	{
		return SuperTools.getInstance()._tw(origina_lw);
	}
	public int _th(int origina_lh)
	{
		return SuperTools.getInstance()._th(origina_lh);
	}
	
	//创建实例对象
	public static ThirdLogin create(RelativeLayout l)
	{
		return new ThirdLogin(l);
	}
	
	//接受回调,游戏loginUI打开
	public static void fromOpenLoginUI(boolean isSubmitParameter)
	{
		//在这里提交从第三方获取到的参数(开发者应该不用修改这里)
		if(isSubmitParameter)
		{
			Toast.makeText(SuperTools.getInstance().getCurActivity(), Text.getKey("Local_EntryGame")/*"进入游戏"*/, Toast.LENGTH_LONG).show();
			CPPManager.getInstance().noticeLoginGame(loginParameter);
		}
		else
			ThirdLogin.create(MainActivity.getCurLayout()).run();	//以后进是呼出第三方登录界面
	}
	
	//接受回调,游戏loginUI中点击登陆按钮(理论上用不到,空在这里先)
	public static void fromClickLoginBtn()
	{
		
	}
	
	//接受回调,游戏loginUI关闭
	public static void fromCloseLoginUI()
	{
		//在这里关闭第三方/自己写的ui控件
		
	}
	
	//接受回调,返回登录成功/失败
	public static void fromLoginStatus(boolean isSuccess)
	{
		L.d("==========LoginStatus:" + String.valueOf(isSuccess));
		if(isSuccess == false)
		{
			//登陆失败,重新登录
			ThirdLogin.create(MainActivity.getCurLayout()).run();
		}
		
	}
		
	//提交参数进入游戏
	public static void entryGame(String data)
	{
		L.d("===========entryGame:" + data);
		loginParameter = data;
		if(MainActivity.getCurPageIndex() != MainActivity.S_Game)
			MainActivity.switchStatus(MainActivity.S_Game);
		else
		{
			MainActivity.getCurLayout().removeAllViews();
			Toast.makeText(SuperTools.getInstance().getCurActivity(), Text.getKey("Local_EntryGame")/*"进入游戏"*/, Toast.LENGTH_LONG).show();
			CPPManager.getInstance().noticeLoginGame(loginParameter);
		}
	}
	
	//登陆点击按钮回调事件
	private OnClickListener btn_Login = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			L.d("yep, I'am loginBtn !");//自定义界面的login按钮
			entryGame("6666");
			
		}
	}; 
	
	public void run() 
	{
		L.d("Begin to deal your business!");
		//---开始业务
		
		
	}


}